﻿@RenderPage("~/Views/Shared/leftMenu.cshtml")
<div id="content-wrapper" ng-controller="UserCtrl">
    <div class="container-fluid">
        <div class="row">
            <div>
                <ol class="breadcrumb">
                    <li><a href="/Admin/index">首页</a></li>
                    <li class="active">用户列表</li>
                    <a href="" ng-click="IHelp()" style="float:right"><i class="icon_question_alt2"></i> 帮助</a>
                </ol>
                <table class="table ListTable helpStep1">
                    <tbody>
                        <tr>
                            <td width="140px">
                                <div class="btn-group">
                                    <a href="/User/Add" class="btn btn-success">新增用户</a>
                                    <button type="button" class="btn btn-success dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
                                        <span class="caret"></span>
                                    </button>
                                    <ul class="dropdown-menu" role="menu">
                                        <li><a href="" ng-click="ICKAll()"><i class="fa fa-hand-paper-o"></i> 全选 / 反选</a></li>
                                        <li role="separator" class="divider"></li>
                                        <li><a href="" ng-click="IDeleteByIDs()"><i class="icon_trash_alt"></i> 删除选中</a></li>
                                        <li><a href="" ng-click="IShowByIDs()"><i class="icon_lock_alt"></i> 解锁选中</a></li>
                                        <li><a href="" ng-click="IHideByIDs()"><i class="icon_lock-open_alt"></i> 锁定选中</a></li>
                                        <li role="separator" class="divider"></li>
                                        <li><a href="/User/Types"><i class="fa fa-list-ol"></i> 分类管理</a></li>
                                    </ul>
                                </div>
                            </td>
                            <td width="150px">
                                <select class="form-control filterSelect" ng-model="Filter.TypeID" ng-change="IData_Get()">
                                    <option value="0">查看全部</option>
                                    <optgroup ng-repeat="d in Types" ng-if="d.PID==0" label="{{d.Name}}">
                                        <option value="{{d.ID}}">全部</option>
                                        <option value="{{dd.ID}}" ng-repeat="dd in Types" ng-if="dd.PID==d.ID">{{dd.Name}}</option>
                                    </optgroup>
                                </select>
                            </td>
                            <td>
                                <div class="input-group">
                                    <span ng-if="Filter.UserName!=null" ng-click="Filter.UserName=null" class="clearBtn animated bounceIn" data-icon="Q"></span>
                                    <input type="text" ng-keypress="OnEnterPress($event);" class="form-control" placeholder="输入用户名..." ng-model="Filter.UserName" />
                                </div>
                            </td>
                            <td>
                                <div class="input-group" style="display:none;">
                                    <div class="input-group-addon">时间</div>
                                    <input type="date" class="form-control" ng-model="Filter.StartTime" />
                                    <div class="input-group-addon">至</div>
                                    <input type="date" class="form-control" ng-model="Filter.EndTime" />
                                </div>
                            </td>
                            <td width="130px">
                                <filter-btn></filter-btn>
                            </td>
                            <td width="90px">
                                <table-pager-top></table-pager-top>
                            </td>
                        </tr>
                    </tbody>
                </table>
                <loading></loading>
                <table id="MainTable" ng-if="loading==false&&Pager.Data.length>0" ng-class="{fadeIn:loading==false}" class="table table-bordered table-striped table-hover animated helpStep2">
                    <thead>
                        <tr>
                            <th width="40px"><input type="checkbox" ng-click="ICKAll()" ng-checked="ckAll==true" /></th>
                            <th ng-repeat="d in TableHeads" ng-click="Filter.OrderBy=d.Name;Filter.IsDesc=!Filter.IsDesc;IData_Get();">
                                {{d.Title}}
                                <i ng-if="Filter.IsDesc==true&&Filter.OrderBy==d.Name" class="arrow_carrot-up"></i>
                                <i ng-if="Filter.IsDesc!=true||Filter.OrderBy!=d.Name" class="arrow_carrot-down"></i>
                            </th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr ng-repeat="d in Pager.Data" ng-class="{warning:d.LockFlag!=1}">
                            <td><input type="checkbox" value="{{d.ID}}" class="EntityItem" ng-checked="ckAll==true" /></td>
                            <td>{{$index+1}}</td>
                            <td>{{d.UserName}}</td>
                            <td>
                            <span ng-repeat="x in Roles" ng-if="d.Role==x.ID">{{x.Name}}</span>
                            </td>
                            <td title="{{d.WechatName}}">{{d.WechatNumber}}</td>
                            <td>{{d.Mobile}}</td>
                            <td>{{d.CreateDate.replace("T"," ").substr(0,10)}}</td>
                            <td>
                                <a ng-if="d.LockFlag==1" href="javascript:void(0)" ng-click="User_Lock(d)" class="tip" title="锁定"><i class="icon_lock-open_alt"></i></a>
                                <a ng-if="d.LockFlag==0" href="javascript:void(0)" ng-click="User_UnLock(d)" class="tip" title="解锁"><i class="icon_lock_alt"></i></a>
                                <a href="/User/Edit/{{d.ID}}" class="tip" title="编辑"><i class="icon_pencil"></i></a>
                                <a href="javascript:void(0)" ng-click="ConfirmDelete(d.ID)" class="tip" title="删除"><i class="icon_trash_alt"></i></a>
                            </td>
                        </tr>
                    </tbody>
                </table>
                <table-pager></table-pager>
                <dialog-delete></dialog-delete>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    angular.module("app").controller("UserCtrl", function ($location, $scope, API_User) {

        $scope.ckAll=false;
        $scope.ICKAll=function()
        {
            $scope.ckAll=!$scope.ckAll;
        };

        $scope.TableHeads = [
            { Title: "编号", Name: "ID" },
            { Title: "用户名", Name: "LoginName" },
            { Title: "角色", Name: "Role" },
            { Title: "微信号", Name: "WechatNumber" },
            { Title: "手机", Name: "Mobile" },
            { Title: "创建时间", Name: "CreateDate" }
        ];

        $scope.Pager = { PageIndex: 0, PageSize: 20 };
        $scope.Filter = { IsDesc: true, OrderBy: "ID" };
        $scope.IData_Get = function () {
            NProgress.start();
            $scope.loading = true;
            API_User.Get($scope.Pager.PageIndex,
                $scope.Pager.PageSize,
                $scope.Filter.UserName,
                $scope.Filter.StartTime,
                $scope.Filter.EndTime,
                $scope.Filter.OrderBy,
                $scope.Filter.IsDesc,
                $scope.Filter.Role).then(function (r) {
                    $scope.loading = false;
                    NProgress.done();
                    $scope.Pager = r;
                    $scope.Pager.PageRange = APP.Range(r.PageCount);
                    setTimeout(function () { $(".tip").tooltip(); }, 500);

                });
        }
        $scope.IData_Get();
        $scope.Types = @MvcHtmlString.Create(ViewBag.Roles);

        $scope.ConfirmDelete = function (id) {
            $scope.DeleteID = id;
            $("#DeleteConfirmModal").modal("show");
        }
        $scope.IDelete = function () {
            $("#DeleteConfirmModal").modal("hide");
            API_User.Delete($scope.DeleteID).then(function () { $scope.IData_Get(); toastr.info('删除成功！'); });
        }
        $scope.IDeleteByIDs=function()
        {
            var cks = $(".EntityItem:checked");
            var cksIDs = new Array();
            $(cks).each(function (a, b) { cksIDs.push($(b).val()); });

            if(cksIDs.length<1)
            {  toastr.error('未选择任何用户。'); return;}

            $scope.loading = true;
            API_User.DeleteByIDs(cksIDs.join(",")).then(function () 
            {
                $scope.loading = false;
                toastr.info('删除成功！'); 
                $scope.IData_Get();
            });
        }
        $scope.IShowByIDs=function()
        {
            var cks = $(".EntityItem:checked");
            var cksIDs = new Array();
            $(cks).each(function (a, b) { cksIDs.push($(b).val()); });
            if(cksIDs.length<1)
            {  toastr.error('未选择任何用户。'); return;}
            $scope.loading = true;
            API_User.ShowByIDs(cksIDs.join(",")).then(function () 
            {
                $scope.loading = false;
                toastr.info('设置成功！'); 
                $scope.IData_Get();
            });
        }
        $scope.IHideByIDs=function()
        {
            var cks = $(".EntityItem:checked");
            var cksIDs = new Array();
            $(cks).each(function (a, b) { cksIDs.push($(b).val()); });
            if(cksIDs.length<1)
            {  toastr.error('未选择任何用户。'); return;}
            $scope.loading = true;
            API_User.HideByIDs(cksIDs.join(",")).then(function () 
            {
                $scope.loading = false;
                toastr.info('设置成功！'); 
                $scope.IData_Get();
            });
        }

        $scope.User_Lock = function (_user) {
            _user.LockFlag = 0;
            API_User.Put(_user).then(function () {
                toastr.info('锁定成功！');
            });
        }
        $scope.User_UnLock = function (_user) {
            _user.LockFlag = 1;
            API_User.Put(_user).then(function () { toastr.info('解锁成功！'); });
        }
        $scope.OnEnterPress = function (evt) {
            if (evt.keyCode == 13) {
                $scope.IData_Get();
            }
        }

        $scope.IHelp = function () {
            var intro = introJs();
            intro.setOptions({
                nextLabel: '下一步 <i class="arrow_carrot-right"></i>',
                prevLabel: ' <i class="arrow_carrot-left"></i> 上一步',
                skipLabel: '关闭',
                doneLabel: '关闭',
                showProgress: false,
                overlayOpacity: 0.4,
                steps: [
                  {
                      intro: '<h4>用户</h3><p>系统后台操作人员，每个用户都能绑定各自的微信、支付宝等平台。</p>',
                  },
                  {
                      position: "bottom",
                      element: '.helpStep1',
                      intro: "<p>批量删除/显示/隐藏用户，分类管理，根据分类筛选、查询、翻页操作栏。</p>"
                  },
                  {
                      position: "top",
                      element: '.helpStep2',
                      intro: "<p>用户列表，您冻结/恢复，修改、删除单个用户。</p>"
                  },
                  {
                      position: "top",
                      element: '.helpStep3',
                      intro: '<p>用户数量统计，翻页操作。</p>'
                  }
                ]
            });
            intro.start();
        };
    });
</script>